package com.daxia.bumall.web.controller.m;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.daxia.bumall.common.MyJSONObject;
import com.daxia.bumall.common.NeedLogin;
import com.daxia.bumall.common.Sex;
import com.daxia.bumall.dto.TimelineCommentDTO;
import com.daxia.bumall.dto.TimelineDTO;
import com.daxia.bumall.dto.TimelineLikeDTO;
import com.daxia.bumall.dto.UserDTO;
import com.daxia.bumall.model.Timeline;
import com.daxia.bumall.model.TimelineComment;
import com.daxia.bumall.model.TimelineLike;
import com.daxia.bumall.model.User;
import com.daxia.bumall.support.MPage;
import com.daxia.bumall.util.DateUtils;

@Controller
@RequestMapping(value = "/m/timeline", produces = "application/json;charset=UTF-8")
public class MTimelineController extends MBaseController {
    

    @ResponseBody
    @RequestMapping("list")
    public String list(TimelineDTO dto, MPage page) throws Exception {
		MyJSONObject data = new MyJSONObject();
    	JSONArray array = new JSONArray();
    	List<TimelineDTO> list = timelineService.find(dto, page);
    	for (TimelineDTO timelineDTO : list) {
    		
    		JSONObject obj = new JSONObject();
    		obj.put("id", timelineDTO.getId());
    		obj.put("userId", timelineDTO.getUser().getId());
    		obj.put("username", timelineDTO.getUser().getUsername());
    		obj.put("nickName", timelineDTO.getUser().getNickName());
    		obj.put("sexRemark", Sex.getByValue(timelineDTO.getUser().getSex()).getRemark());
    		obj.put("content", timelineDTO.getContent());
    		obj.put("headImage", timelineDTO.getUser().getHeadImage());
    		obj.put("publishTime", DateUtils.format(timelineDTO.getCreateTime(), "yyyy-MM-dd HH:mm"));
    		
    		String[] imgs = timelineDTO.getImages().split(",");
    		if (imgs != null && imgs.length > 9) {
    			imgs = ArrayUtils.subarray(imgs, 0, 9);
    		}
    		obj.put("images", imgs);
    		
    		JSONArray likes = new JSONArray();
    		
    		for (TimelineLike tl : timelineDTO.getTimelineLikes()) {
    			JSONObject likeObj = new JSONObject();
    			
    			likeObj.put("userId", tl.getUser().getId());
    			likeObj.put("username", tl.getUser().getUsername());
    			likeObj.put("nickName", tl.getUser().getNickName());
    			likeObj.put("headImage", tl.getUser().getHeadImage());
    			likes.add(likeObj);
    		}
    		
    		JSONArray comments = new JSONArray();
    		for (TimelineComment tc : timelineDTO.getTimelineCommnets()) {
    			
    			JSONObject commentObj = new JSONObject();
    			commentObj.put("userId", tc.getUser().getId());
    			commentObj.put("username", tc.getUser().getUsername());
    			commentObj.put("nickName", tc.getUser().getNickName());
    			commentObj.put("content", tc.getContent());
    			commentObj.put("headImage", tc.getUser().getHeadImage());
    			commentObj.put("createTime", DateUtils.format(tc.getTime(), "yyyy-MM-dd"));
    			comments.add(commentObj);
    		}
    		
    		
    		obj.put("comments", comments);
    		obj.put("likes", likes);
    		array.add(obj);
    	}
		data.put("timelineInfos", array);
		setPageInfo(data, page);
		
		return toJson(data);
    }

    @NeedLogin
    @ResponseBody
	@RequestMapping("publish")
	public String publish(String content, HttpServletRequest request) throws Exception {
    	
		String fileUrl = super.saveFilesAndReturnFileName(request);
		UserDTO currentUser = getCurrentUserDTO();
    	User user = new User();
    	BeanUtils.copyProperties(currentUser, user);
		
		TimelineDTO dto = new TimelineDTO();
		dto.setContent(content);
		dto.setCreateTime(new Date());
		dto.setImages(fileUrl);
		dto.setUser(user);
		timelineService.create(dto );
		return toJson();
	}
    
    @NeedLogin
    @ResponseBody
    @RequestMapping("comment")
    public String comment(String timelineId, String comment) throws Exception {
    	super.assertNotBlank(timelineId, "社交圈ID不能为空");
    	TimelineDTO timelineDTO = timelineService.load(Long.parseLong(timelineId));
    	Timeline timeline = new Timeline();
    	BeanUtils.copyProperties(timelineDTO, timeline);
    	UserDTO currentUser = getCurrentUserDTO();
    	User user = new User();
    	BeanUtils.copyProperties(currentUser, user);
    	
    	TimelineCommentDTO dto = new TimelineCommentDTO();
    	dto.setContent(comment);
    	dto.setTime(new Date());
		dto.setTimeline(timeline );
		dto.setUser(user);
		timelineCommentService.create(dto );
    	
    	return toJson();
    }
    
    
    @NeedLogin
    @ResponseBody
    @RequestMapping("like")
    public String like(String timelineId) throws Exception {
    	super.assertNotBlank(timelineId, "社交圈ID不能为空");
    	User user = super.getCurrentUser();
    	TimelineLikeDTO timelineLikeDTO = timelineLikeService.findByUserIdAndTimelineId(user.getId(), Long.parseLong(timelineId));
    	if (timelineLikeDTO != null) {
    		return toJson();
    	}
    	TimelineDTO timelineDTO = timelineService.load(Long.parseLong(timelineId));
    	if (timelineDTO.getUser().getId().longValue() == user.getId()) {
    		throw new RuntimeException("不能给自己点赞");
    	}
    	Timeline timeline = new Timeline();
    	BeanUtils.copyProperties(timelineDTO, timeline);
    	
    	TimelineLikeDTO dto = new TimelineLikeDTO();
    	dto.setTime(new Date());
    	dto.setTimeline(timeline);
    	dto.setUser(user);
		timelineLikeService.create(dto );
    	
    	return toJson();
    }
    
    
}
